package com.hama.iotrust.common.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.hama.iotrust.common.ui.EasyUITreeNode;
import com.hama.iotrust.common.util.StrUtil;

public class EasyUITree extends ArrayList<EasyUITreeNode> {

    private static final long serialVersionUID = -7789650832106553912L;

    public EasyUITreeNode getChildNodeById(String id) {
        for (EasyUITreeNode subNode : this) {
            // 判断根节点
            if (subNode.getId().equals(id)) {
                return subNode;
            }
            // 在根节点的子节点中查找
            EasyUITreeNode foundSubNode = subNode.getChildNodeById(id);
            if (foundSubNode != null) return foundSubNode;
        }
        return null;
    }

    public List<String> getIdListOfChildrenWithSelf(String nodeId) {
        List<String> result = new ArrayList<String>();
        EasyUITreeNode foundNode = getChildNodeById(nodeId);
        if (foundNode != null) {
            result.addAll(foundNode.getIdListOfChildrenWithSelf());
        }
        return result;
    }

    public void removeNode(String id) {
        for (int i = size() - 1; i >= 0; i--) {
            EasyUITreeNode subNode = get(i);
            if (subNode.getId().equals(id)) {
                remove(subNode);
            }
            subNode.removeNode(id);
        }
    }

    /**
     * 添加节点,自动查找父节点
     * 
     * @param node
     * @param parentId
     */
    public void addNode(EasyUITreeNode node) {
        EasyUITreeNode parentNode = getChildNodeById(node.getParentId());
        if (parentNode != null) {
            parentNode.addChildNode(node);
        } else {
            super.add(node);
        }
        // 判断根节点中是否有所添加节点的子节点，有的话调整节点从属关系
        for (int i = this.size() - 1; i >= 0; i--) {
            EasyUITreeNode rootNode = get(i);
            if (node.getId().equals(rootNode.getParentId())) {
                node.addChildNode(rootNode);
                this.remove(rootNode);
            }
        }
    }

    public boolean add(EasyUITreeNode node) {
        addNode(node);
        return true;
    }

    public void add(int index, EasyUITreeNode node) {
        addNode(node);
    }

    public boolean addAll(Collection<? extends EasyUITreeNode> c) {
        for (EasyUITreeNode node : c) {
            addNode(node);
        }
        return true;
    }

    public boolean addAll(int index, Collection<? extends EasyUITreeNode> c) {
        for (EasyUITreeNode node : c) {
            addNode(node);
        }
        return true;
    }

    public void closeNodeIfHasChildren(int targetLevel) {
        int level = 0;
        closeNodeIfHasChildren(level, targetLevel);
    }

    private void closeNodeIfHasChildren(int level, int targetLevel) {
        for (int i = 0; i < this.size(); i++) {
            EasyUITreeNode node = get(i);
            if (node.hasChildren()) {
                if (level >= targetLevel) node.setState(EasyUITreeNode.STATE_CLOSED);
                node.getChildren().closeNodeIfHasChildren(level++, targetLevel);
            }
        }
    }

    public String toString() {
        return StrUtil.jsonFromObject(this);
    }

}
